//
//  HomePageViewController.m
//  SinceLe
//
//  Created by 李元喜 on 15/11/14.
//  Copyright © 2015年 李元喜. All rights reserved.
//

#import "HomePageViewController.h"
#import "HotTableViewCell.h"
#import "Macrocs.h"
#import "NetWorkHandle.h"
#import <UIImageView+WebCache.h>
#import "HometwoViewController.h"


@interface HomePageViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tabeleView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadtableView];
    [self loadHomeData];
    self.view.backgroundColor = [UIColor whiteColor];
   
}

-(void)loadtableView{
    _tabeleView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tabeleView.delegate = self;
    _tabeleView.dataSource = self;
    
    UINib *nib = [UINib nibWithNibName:@"HotTableViewCell" bundle:nil];
    [_tabeleView registerNib:nib forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:_tabeleView];
    
}
//返回多少个cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

//cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 250;
    
}


//数据解析
-(void)loadHomeData{
    
    NSString *Str = [NSString stringWithFormat:Konerl];
    [NetWorkHandle getDataWithURLString:Str compare:^(id object) {
        _dataArray = [NSMutableArray array];
        NSDictionary *dic = object;
        NSArray *arr = dic[@"data"][@"items"];
        for (NSDictionary *a in arr) {
            MusicModel *model = [[MusicModel alloc]init];
            [model setValuesForKeysWithDictionary:a];
            [_dataArray addObject:model];
        }
        
        [_tabeleView reloadData];
    }];
    
}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    
    
    //赋值
    MusicModel *model = _dataArray[indexPath.row];
    [cell.Oneimage sd_setImageWithURL:[NSURL URLWithString:model.imageurl]];
    cell.Onename.text = model.name;
    cell.OneL.text = [NSString stringWithFormat:@"%ld",(long)model.fav];
    cell.OneP.text = [NSString stringWithFormat:@"%ld",(long)model.comm];
    cell.Onenum.text = [NSString stringWithFormat:@"%ld",(long)model.number];
    
    
    return cell;
    
}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{

    HometwoViewController *twoView = [[HometwoViewController alloc]init];
    
    MusicModel* model = _dataArray[indexPath.row];
    
    twoView.id = model.id;
    
    [self.navigationController pushViewController:twoView animated:YES];
    
    
}
























@end
